package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author gang
 */
@RestController
public class AutoTestWorkFlow {

    @GetMapping("hello")
    public String hello() {
        return "hello";
    }


    public static void main(String[] args) throws InterruptedException {
        AutoTestWorkFlow.execute();
    }

    public static void execute() throws InterruptedException {
        //初始化浏览器驱动
        WebDriver webDriver = AutoTestWorkFlow.initChromeDriver();
        //添加鼠标事件
        Actions action = new Actions(webDriver);
        //打开登录页
        webDriver.get("http://ninemax.wicp.net:8010/login");
        Thread.sleep(1000);
        //登录系统
        AutoTestWorkFlow.loginSystem(webDriver);
        Thread.sleep(1000);
        //进入工作流
        AutoTestWorkFlow.openWorkFlow(webDriver);
        Thread.sleep(1000);
        //获取组件库
        List<WebElement> workFlowElements = webDriver.findElements(By.className("x6-graph-svg-stage"));
        //选择数据读取种第一个元素，并上传数据
        AutoTestWorkFlow.dealDataReader(webDriver, workFlowElements, action);
        Thread.sleep(1000);
        //选择数据预处理中第一个元素
        AutoTestWorkFlow.dealDataClear(workFlowElements, action);
        Thread.sleep(1000);
        //获取画布中组件
        List<WebElement> containerElements = webDriver.findElement(By.cssSelector("#container .x6-graph-svg-stage")).findElements(By.tagName(("g")));
        //处理连线
        AutoTestWorkFlow.connectLine(action, containerElements);
        Thread.sleep(1000);
        //选择画布中组件-数据清洗，并添加组件配置
        AutoTestWorkFlow.addClearConfigs(webDriver, action, containerElements);
        Thread.sleep(1000);
        //保存
        webDriver.findElement(By.xpath("//button[@name='toJSON']")).click();
        Thread.sleep(1000);
        //运行工作流
        webDriver.findElement(By.xpath("//button/span[text()='运行工作流 ']")).click();

        //执行JS方法将视图滚动至中央
        //WebElement container = webDriver.findElement(By.id("container"));
        //((JavascriptExecutor) webDriver).executeScript("arguments[0].scrollIntoView({block:'center'});", container);
        //关闭
        //webDriver.close();

    }

    public static WebDriver initChromeDriver() {
        System.setProperty("webdriver.chrome.driver", "d://soft//ChromeDriver//chromedriver.exe");
        WebDriver webDriver = new ChromeDriver();
        webDriver.manage().window().maximize();
        webDriver.manage().deleteAllCookies();
        // 与浏览器同步非常重要，必须等待浏览器加载完毕
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        return webDriver;
    }

    public static void loginSystem(WebDriver webDriver) {
        //输入账号 密码并登陆系统
        List<WebElement> inputElements = webDriver.findElements(By.tagName("input"));
        inputElements.get(0).sendKeys("lfy");
        inputElements.get(1).sendKeys("Admin123!@#");
        webDriver.findElement(By.cssSelector(".login_form button")).click();
    }

    public static void openWorkFlow(WebDriver webDriver) throws InterruptedException {
        webDriver.get("http://ninemax.wicp.net:8010/xFlowTest/6370554086148096");
        Thread.sleep(2000);
        // 清空画布
        webDriver.findElement(By.xpath("//button[@name='clear']")).click();
        Thread.sleep(200);
        webDriver.findElement(By.className("el-message-box__btns")).findElements(By.tagName("button")).get(1).click();
    }

    public static void dealDataReader(WebDriver webDriver, List<WebElement> elements, Actions action) throws InterruptedException {
        WebElement element = elements.get(0).findElements(By.tagName("g")).get(0);
        System.out.println("数据读取第一个元素 = " + element.getText());
        //拖拽元素到指定位置
        action.dragAndDropBy(element, 400, 40).click().perform();
        Thread.sleep(1000);
        //上传文件
        AutoTestWorkFlow.uploadFile(webDriver);
        //选择文件
        //AutoTestWorkFlow.selectFile(webDriver);
    }

    public static void uploadFile(WebDriver webDriver) {
        //上传文件，不用执行点击按钮操作直接选本地文件上传并利用sendKeys赋值给input就行
        File file = new File("C://Users//ninemax//Desktop//test_file//数据导入映射//jg.xlsx");
        if (file.exists()) {
            webDriver.findElement(By.name("file")).sendKeys(file.getPath());
        }
    }

    public static void selectFile(WebDriver webDriver) throws InterruptedException {
        webDriver.findElement(By.xpath("//div[@class='select_upload']/button")).click();
        Thread.sleep(1000);
        //选中已上传
        List<WebElement> elements = webDriver.findElements(By.xpath("//table/tbody/tr"));
        List<WebElement> tds = elements.get(0).findElements(By.tagName("td"));
        tds.get(tds.size() - 1).findElement(By.tagName("button")).click();
        Thread.sleep(1000);
    }

    public static void dealDataClear(List<WebElement> elements, Actions action) {
        WebElement element = elements.get(1).findElements(By.tagName("g")).get(0);
        System.out.println("数据预处理第一个元素 = " + element.getText());
        // 拖拽元素到指定位置
        action.dragAndDropBy(element, 400, 80).click().perform();
    }

    public static void connectLine(Actions action, List<WebElement> elements) {
        String elementText = "数据读取";
        WebElement dataReaderPoint = elements.stream().filter(e -> elementText.equals(e.getText())).findFirst().get().findElement(By.tagName(("g")));
        //yOffset值134是灵活的，根据实际情况算
        action.clickAndHold(dataReaderPoint).moveByOffset(0, 134).release().perform();
    }

    public static void addClearConfigs(WebDriver webDriver, Actions action, List<WebElement> elements) throws InterruptedException {
        WebElement containerElementClear = elements.stream().filter(e -> "数据清洗".equals(e.getText())).findFirst().get();
        action.click(containerElementClear).perform();
        Thread.sleep(1000);
        //选中下拉框
        List<WebElement> clearConfigElements = webDriver.findElements(By.cssSelector(".xFlow_form_clean_content .xFlow_form_clean_row"));
        WebElement clearConfigElement = clearConfigElements.get(0);
        //输入值吊起选择框，el-select不是select标签所以无法使用selenium的select类选值，只能手动
        List<WebElement> selectElements = clearConfigElement.findElements(By.tagName("input"));
        //设置清洗字段，这个清洗字段灵活修改
        String filedValue = "title";
        selectElements.get(0).sendKeys(filedValue);
        //这个时候下拉框就可以找到了，然后选中这个值
        webDriver.findElement(By.xpath("//ul/li/span[text()='" + filedValue + "']")).click();
        Thread.sleep(500);
        //设置清洗规则
        String ruleValue = "非空";
        selectElements.get(1).sendKeys(ruleValue);
        //这个时候下拉框就可以找到了，然后选中这个值
        webDriver.findElement(By.xpath("//ul/li/span[text()='" + ruleValue + "']")).click();
    }
}
